package cn.dujc.superkid.activities;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.TextView;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.manager.PieChartManagger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cn.dujc.core.ui.BaseActivity;
import cn.dujc.core.util.TextColorBuilder;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.adapter.UnitTestLegendAdapter;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.ContactResult;
import cn.dujc.superkid.bean.MyExamineResult;
import cn.dujc.superkid.bean.ProcessingAnswersResult;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.bean.ScoreStatisticsEntity;
import cn.dujc.superkid.bean.UnitResult;
import cn.dujc.superkid.utils.Preferences;
import retrofit2.Call;
import retrofit2.http.QueryMap;

public class MyTestResultsActivity extends BaseActivity {
    public static final String EXTRA_MYTEST = "EXTRA_COURSE";
    private MyExamineResult.MyExamineEntity myExamineEntity;
    private String achievement;
    private final List<ProcessingAnswersResult> list = new ArrayList<>();
    private TextView tv_info, tv_title_0, tv_title_2;
    private PieChart mp_pieChart;
    private List<PieEntry> entries = new ArrayList<>();
    private List<Integer> colors = new ArrayList<>();
    RecyclerView recyclerView;

    @Override
    public int getViewId() {
        return R.layout.activity_my_test_results;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        myExamineEntity = extras().get(EXTRA_MYTEST);
        setTitle("考试结果");
        loadData();

    }

    private void initViews(ProcessingAnswersResult processingAnswersResult) {
        recyclerView = findViewById(R.id.my_result_recycler);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        UnitTestLegendAdapter adapter = new UnitTestLegendAdapter(this);
        recyclerView.setAdapter(adapter);
        List<UnitResult.DataBean.ScoreStatisticsBean> datas = new ArrayList<>();
        for (int i = 0; i < processingAnswersResult.getScore_statistics().size(); i++) {
            datas.add(new UnitResult.DataBean.ScoreStatisticsBean(
                    processingAnswersResult.getScore_statistics().get(i).getGroup_name(),
                    Double.parseDouble(processingAnswersResult.getScore_statistics().get(i).getScore()),
                    processingAnswersResult.getScore_statistics().get(i).getColor()
            ));
        }
        adapter.setDatas(datas);


        tv_title_0 = findViewById(R.id.tv_title_0);
        tv_title_2 = findViewById(R.id.tv_title_2);
        mp_pieChart = findViewById(R.id.mp_pieChart);
        tv_info = findViewById(R.id.tv_info);
        tv_title_0.setText(processingAnswersResult.getPage_info().getExamine_title());
        int color = ContextCompat.getColor(mActivity, R.color.orange);
        if (TextUtils.isEmpty(processingAnswersResult.getPage_info().getExamine_score())) {
            achievement = "0";
        } else {
            achievement = processingAnswersResult.getPage_info().getExamine_score();
        }

        tv_info.setText(new TextColorBuilder()
                .addTextPart("本试卷共")
                .addTextPart(color, processingAnswersResult.getPage_info().getNum())
                .addTextPart("题，共计100分，第一次得分为")
                .addTextPart(color, achievement)
                .addTextPart("分，本次得分")
                .addTextPart(color, processingAnswersResult.getPage_info().getAchievement())
                .addTextPart("分")
                .build());
        tv_title_2.setText(new TextColorBuilder()
                .addTextPart("总答题人数")
                .addTextPart(color, processingAnswersResult.getPage_info().getAnswer_num())
                .addTextPart("人，回答情况如下：")
                .build());
        initPieChartData(processingAnswersResult.getScore_statistics());
    }

    private void initPieChartData(List<ScoreStatisticsEntity> score_statistics) {
        for (int i = 0; i < score_statistics.size(); i++) {
            entries.add(new PieEntry(Float.parseFloat(score_statistics.get(i).getScore()), score_statistics.get(i).getGroup_name()));
            colors.add(Color.parseColor(score_statistics.get(i).getColor()));
        }
        PieChartManagger pieChartManagger = new PieChartManagger(mp_pieChart);
        pieChartManagger.showSolidPieChart(entries, colors);


    }

    private void loadData() {
        if (!Preferences.isLogin(this)) {
            starter().go(LoginActivity.class);
        }
        final RegisterResult loginResult = Preferences.getInstance().getLoginResult(mActivity);
        HttpHelper.getUserCenter().memberProcessingAnswers(QMap.build()
                .put("user_id", loginResult.getUid())
                .put("channel", Preferences.getInstance().getChannel(mActivity).getChannel_id())
                .put("logincode", loginResult.getLogincode())
                .put("examine_id", myExamineEntity.getId())
                .put("type_id", myExamineEntity.getType())
                .create()).enqueue(new ResultCallBack<Result<ProcessingAnswersResult>>(mActivity) {
            @Override
            public void onSuccessCallback(int code, Result<ProcessingAnswersResult> body) {
                list.clear();
                final ProcessingAnswersResult data = body.getData();
                initViews(data);
            }
        });
    }
}
